package logic

import (
	"box-fresh-api/models/mysql"
	"context"

	"box-fresh-api/boxrpc/boxrpc"
	"box-fresh-api/boxrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type TypeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTypeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TypeAddLogic {
	return &TypeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TypeAddLogic) TypeAdd(in *boxrpc.TypeAddRequest) (*boxrpc.TypeAddResponse, error) {
	// todo: add your logic here and delete this line
	var types mysql.Type
	if err := types.TypeFind(mysql.DB, in.TypeId); err != nil {
		types = mysql.Type{
			Model:    gorm.Model{},
			TypeName: in.TypeName,
		}

		err = types.TypeAdd(mysql.DB)
		if err != nil {
			return nil, err
		}

	}
	return &boxrpc.TypeAddResponse{
		TypeId: int64(types.ID),
	}, nil
}
